package com.mxx.democharts2.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 根据年月号（2016 2） 推算该月结束时间
     */
    public static Date getMonthEnd(int year, int mouth){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, mouth-1);

        int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH);  // 当月的最后一天 28 29 30 31
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);

        return c.getTime();
    }

    /**
     * 根据年月号（2016 2） 推算该月开始时间
     */
    public static Date getMonthStart(int year, int mouth){
        Calendar c=Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, mouth-1);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 1, 0, 0, 0);

        return c.getTime();
    }

    /**
     * 根据Date获取当月的天号（1-31）
     */
    public static int getMouthDay(Date date){
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);

        int day = Integer.parseInt(dateStr.split("-")[2]);

        return day;
    }


    public static void main(String[] args) {


       /* int year = 2016;
        int mouth = 2;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date monthEnd = DateUtils.getMonthEnd(year, mouth);
        Date monthStart = DateUtils.getMonthStart(year, mouth);
        String start = sdf.format(monthStart);
        String end = sdf.format(monthEnd);


        System.out.println(start+"--"+end);*/

        int mouthDay = DateUtils.getMouthDay(new Date());
        System.out.println(mouthDay);
    }
}
